package com.xiangyang.service.impl;

import com.xiangyang.entity.*;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.mapper.IPatientsMapper;
import com.xiangyang.service.IRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
public class RegistrationImpl implements IRegistrationService {
    @Autowired
    private IPatientsMapper patientsMapper;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private Random random = new Random();

    @Override
    public Result addPatientDetails(PatientInformation patientInformation) {
        System.out.println(patientInformation);

//        try {
            if (patientInformation == null || patientInformation.getPatientName() == null || patientInformation.getPatientPhone() == null) {
            return Result.error("角色信息不完整");
        }

        if (patientsMapper.selectPatientByName(null, patientInformation.getPatientName(), patientInformation.getPatientPhone())== null) {
            patientsMapper.insertPatient(patientInformation.getPatientName(), patientInformation.getPatientPhone());
        }
        LocalDateTime now = LocalDateTime.now(); // 获取当前日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        PatientDetails patientDetails = new PatientDetails();
        patientDetails.setPatientsId((patientsMapper.selectPatientByName(null, patientInformation.getPatientName(), patientInformation.getPatientPhone())).getPatientId());
        patientDetails.setAge(patientInformation.getAge());
        patientDetails.setGender(patientInformation.getGender());
        patientDetails.setIdentityCard(patientInformation.getIdentityCard());
        patientDetails.setAddress(patientInformation.getAddress());
        patientDetails.setMedicalHistory(patientInformation.getMedicalHistory());
        patientDetails.setAllergen(patientInformation.getAllergen());
        patientDetails.setCreatedAt(now.format(formatter));
        patientDetails.setStatusId(1L);
        if (patientDetails == null || patientDetails.getAge() == null ||
                patientDetails.getGender() == null ||
                patientDetails.getIdentityCard() == null ||
                patientDetails.getAddress() == null ||
                patientDetails.getMedicalHistory() == null || patientDetails.getAllergen() == null) {
            return Result.error("角色信息不完整");
        }

        return Result.ok(patientsMapper.addPatientDetails(patientDetails));
//        }catch (Exception e){
//            return Result.error(e.getMessage());
//        }

    }

    @Override
    public Result updatePatientDetails(PatientInformation patientInformation) {
        try {
            if(patientInformation == null){
            return Result.error("角色信息不完整");
        }
        LocalDateTime now = LocalDateTime.now(); // 获取当前日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        PatientDetails patientDetails = new PatientDetails();
        return Result.ok(patientsMapper.updatePatientDetails(patientInformation.getPatientsId(),1L,patientDetails,now.format(formatter)));
        }catch (Exception e){
            return Result.error("角色信息异常");
        }
    }

    @Override
    public Result getPatientDetailsById() {
        try {
            return Result.ok(patientsMapper.selectCaseHistoryByPatientId(1L));
        }catch (Exception e){
             return Result.error("获取异常");
        }
    }

    @Override
    public Result getPatientDetailsById(String patientName, String patientPhone) {
        try {
            if (patientName == null || patientPhone == null) {
            return Result.error("角色信息不完整");
        }
        Patients patients = patientsMapper.selectPatientByName(null, patientName, patientPhone);
        System.out.println(patients);
        if(patients.getPatientId()==null){
            return Result.error("未找到该用户");
        }
        PatientInformation patientInformation = new PatientInformation();
        PatientDetails patientDetails = patientsMapper.selectPatientDetailsById(1L, patientsMapper.selectPatientByName(null, patientName, patientPhone).getPatientId());
        if (patientDetails == null || patientDetails.getPatientsId() == null) {
            return Result.error("未找到该用户");
        }
        patientInformation.setPatientsId(patientDetails.getPatientsId());
        patientInformation.setDoctorId(patientDetails.getDoctorId());
        patientInformation.setPatientName(patientName);
        patientInformation.setPatientPhone(patientPhone);
        patientInformation.setAge(patientDetails.getAge());
        patientInformation.setGender(patientDetails.getGender());
        patientInformation.setIdentityCard(patientDetails.getIdentityCard());
        patientInformation.setAddress(patientDetails.getAddress());
        patientInformation.setMedicalHistory(patientDetails.getMedicalHistory());
        patientInformation.setAllergen(patientDetails.getAllergen());
        patientInformation.setUserName(patientsMapper.selectEmployeeByemployeeId(patientDetails.getDoctorId()).getUserName());
        patientInformation.setDepartmentName(patientsMapper.selectEmployeeByemployeeId(patientDetails.getDoctorId()).getDepartmentName());
        patientInformation.setCreatedAt(patientDetails.getCreatedAt());
        return Result.ok(patientInformation);
        }catch (Exception e){
            return Result.error("未找到该用户");
        }
    }

    @Override
    public Result addCaseHistory(Long PatientId, String symptom, String Diagnosis) {
        try {
            if (PatientId == null || symptom == null || Diagnosis == null){
            return Result.error("角色信息不完整");
        }
        PatientDetails patientDetails = patientsMapper.selectPatientDetailsById(1L, PatientId);
        if (patientDetails == null){
            return Result.error("未找到该患者");
        }
        CaseHistory caseHistory = new CaseHistory();
        caseHistory.setPatientId(patientDetails.getPatientsId());
        caseHistory.setRegistrationId(patientDetails.getRegistrationId());
        caseHistory.setDoctorId(patientDetails.getDoctorId());
        caseHistory.setSymptom(symptom);
        caseHistory.setDiagnosis(Diagnosis);
        LocalDateTime now = LocalDateTime.now(); // 获取当前日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        caseHistory.setCreatedAt(now.format(formatter));
        patientsMapper.insertCaseHistory(caseHistory);
        return Result.ok();
        }catch (Exception e){
            return Result.error("未找到该患者");
        }
    }

    @Override
    public Result updateCaseHistory(Long caseHistoryId,String symptom,String Diagnosis) {
        try {
            if (caseHistoryId == null || symptom == null || Diagnosis == null){
            return Result.error("角色信息不完整");
        }
        return Result.ok(patientsMapper.updateCaseHistoryByPatientId(caseHistoryId, symptom, Diagnosis,null));
        }catch (Exception e){
            return Result.error("角色信息异常");
        }
    }

    @Override
    public Result addPrescription(PatientRegistration patients) {
        try {
            if (patients == null || patients.getPatientName() == null || patients.getPatientPhone() == null) {
                System.out.println(patients.getPatientPhone());
            return Result.error("角色信息不完整");
        }
        PatientDetails patientDetails = patientsMapper.selectPatientDetailsById(1L, patientsMapper.selectPatientByName(null, patients.getPatientName(), patients.getPatientPhone()).getPatientId());
        if (patientDetails == null || patientDetails.getPatientsId() == null){
            return Result.error("未找到该用户");
        }
        if (patientsMapper.updateCaseHistoryByPatientId(patientDetails.getRegistrationId(),null,null,patients.getRemark())<0){
            return Result.error("未修改");
        }
        CaseHistory caseHistory = new CaseHistory();
        caseHistory.setPrescriptionId(patientDetails.getRegistrationId());
        LocalDateTime now = LocalDateTime.now(); // 获取当前日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < patients.getPrescriptionList().size(); i++) {
            Prescription prescription = patients.getPrescriptionList().get(i);
            prescription.setPrescriptionId(patientDetails.getRegistrationId());
            prescription.setCreatedAt(now.format(formatter));
            // 在这里对每个prescription对象进行操作
            patientsMapper.insertPrescription(prescription);
        }
        return Result.ok();
        }catch (Exception e){
            return Result.error("未找到该用户");
        }

    }

    @Override
    public Result getPrescriptionByRegistrationId(String PatientName, String PatientPhone) {
        try {
            if (PatientName == null || PatientPhone == null) {
                return Result.error("角色信息不完整");
            }
            System.out.println(1);

            PatientDetails patientDetails = patientsMapper.selectPatientDetailsById(1L, patientsMapper.selectPatientByName(null, PatientName, PatientPhone).getPatientId());
            Long caseHistoryId = patientsMapper.selectCaseHistoryByRegistrationId(patientDetails.getRegistrationId());
            System.out.println(1);
            if (caseHistoryId == null) {
                return Result.error("角色信息不完整");
            }
            List<CaseHistoryTion> caseHistoryList = patientsMapper.selectCaseHistoryByIdList(caseHistoryId);
            System.out.println(1);
            if (caseHistoryList.size() == 0) {
                return Result.error("暂无数据");
            }
            return Result.ok(caseHistoryList);

        } catch (NullPointerException e){
            return Result.error("未找到该用户");
        }

    }
    @Override
    public Result getallPrescription() {
        try {
            List<CaseHistoryTion> caseHistoryList = patientsMapper.selectCaseHistoryByIdList(null);
        if(caseHistoryList.size()==0){
            return Result.error("暂无数据");
        }
        return Result.ok(caseHistoryList);
        }catch (Exception e){
            return Result.error("请求异常");
        }
    }

    @Override
    public Result getprescription(Long caseHistoryId) {
        try {
            if (caseHistoryId==null){
            return Result.error("角色信息不完整");
        }
        return Result.ok(patientsMapper.selectPrescriptionByPrescriptionId(caseHistoryId));
        }catch (Exception e){
            return Result.error("请求异常");
        }
    }
}
